package org.scboy.gateway.filters;

import org.springframework.core.Ordered;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;

import java.io.File;

import org.scboy.gateway.decorator.ExportDecorator;
import org.scboy.gateway.entity.ExportType;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.web.server.ServerWebExchange;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import reactor.core.publisher.Mono;

/**
 * 导出数据拦截
 * @author 杨浩
 */
@Configuration
public class ExportGlobalFilter implements GlobalFilter, Ordered {
	
	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		
		
		//判断是否有导出请求头
		String exportConfig = exchange.getRequest().getHeaders().getFirst("Bmps-Export-Config");

		//处理到处数据
		if(StrUtil.isNotEmpty(exportConfig)) {
			ExportType type=new JSONObject(Base64.decodeStr(exportConfig)).toBean(ExportType.class);
			
			//获取模版文件
			File templateFile=null;
			//返回值处理
			ExportDecorator decorator = new ExportDecorator(templateFile,type,exchange.getRequest(), exchange.getResponse());
			return chain.filter(exchange.mutate().response(decorator).build());
		}else {
			
		}
		return chain.filter(exchange);
	}

	@Override
	public int getOrder() {
		return -900;
	}
}